package com.bdqn.bookshop.controller.admin;

import com.bdqn.bookshop.common.BookConstants;
import com.bdqn.bookshop.entity.Classify;
import com.bdqn.bookshop.entity.Setting;
import com.bdqn.bookshop.service.ClassifyService;
import com.bdqn.bookshop.service.SettingService;
import com.bdqn.bookshop.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author : 罗若然[LUO_RUO_RAN]
 * @date : 2020-07-24 21:05
 **/
@Controller
@RequestMapping("/admin")
public class AdminViewController {

    @Autowired
    private ClassifyService classifyService;
    @Autowired
    private SettingService settingService;

    @RequestMapping("/{view}.html")
    public String index(@PathVariable("view") String view, Model model,HttpSession session){
        Setting setting = settingService.loadSetting();
        session.setAttribute("settingModel",setting);
        if(view.equals("classify")){
            List<Classify> classifyParent = classifyService.getClassifyParent();//查询所有一级分类
            model.addAttribute("classifyParent",classifyParent);
        }
        if(view.equals("books")){
            List<Classify> classifyNode = classifyService.getClassifyNode();
            model.addAttribute("classifyNode",classifyNode);
        }

        return  "admin/"+view;
    }



    @GetMapping("/logout")
    public String logout(HttpSession session){
        if (session.getAttribute(BookConstants.LOGIN_ADMIN_USER)!=null){
            session.removeAttribute(BookConstants.LOGIN_ADMIN_USER);
        }
        return "redirect:admin/login";
    }
}
